import numpy as np
import pandas as pd

'''
nan的用法
'''


def main():
    n = np.nan
    # float
    print(type(n))

    m = 1
    # nan
    print(m + n)

    '''
    Series
    '''
    '''
    A    1.0
    B    2.0
    C    NaN
    D    3.0
    E    4.0
    dtype: float64
    '''
    s1 = pd.Series([1, 2, np.nan, 3, 4], index=['A', 'B', 'C', 'D', 'E'])
    print(s1)

    '''
    A    False
    B    False
    C     True
    D    False
    E    False
    dtype: bool
    '''
    print(pd.isnull(s1))

    '''
    A     True
    B     True
    C    False
    D     True
    E     True
    dtype: bool
    '''
    print(pd.notnull(s1))

    '''
    A    1.0
    B    2.0
    D    3.0
    E    4.0
    dtype: float64
    '''
    print(s1.dropna())


    '''
    dataframe
    '''
    df = pd.DataFrame([[1,2,3],[np.nan,5,6],[7,np.nan,9],[np.nan,np.nan,np.nan]])
    '''
         0    1    2
    0  1.0  2.0  3.0
    1  NaN  5.0  6.0
    2  7.0  NaN  9.0
    3  NaN  NaN  NaN
    '''
    print(df)
    print(pd.isnull(df))
    print(pd.notnull(df))

    '''
    axis为0按行删，为1按列删
    how为any有一个就删，all全为nan才删
    thresh=n，保留至少有n个非NA数的行
    '''
    print(df.dropna(axis=0, how='any', thresh=1))

    '''
    nan全填充1
    '''
    print(df.fillna(1))
    '''
    nan按照列填充
    '''
    print(df.fillna(value={0:0,1:1,2:2}))

if __name__ == '__main__':
    main()
